<?php
include 'check.php';

$ident;
$nome;
$sobrenome;
$telefone1;
$telefone2;
$cpf;
$rg;
$email;
$plano;
$alergias;
$deficiencias;

//Pesquisa por CPF
if (!empty($_POST["cpf"])) {

    // Extrair dados de acordo com o id passado no URL
    $strSQL = "SELECT * FROM pacientes WHERE cpf=" . $_POST["cpf"];
    $rs = mysql_query($strSQL);
    $row = mysql_fetch_array($rs);
    
    $ident = $row["id"];
    $nome = $row["Nome"];
    $sobrenome = $row["Sobrenome"];
    $telefone1 = $row["Telefone1"];
    $telefone2 = $row["Telefone2"];
    $cpf = $row["CPF"];
    $rg = $row["RG"];
    $email = $row["Email"];
    $plano = $row["Plano_de_Saude"];
    $alergias = $row["Alergias"];
    $deficiencias = $row["Deficiencias"];
}

//Pesquisa por Nome
if (!empty($_POST["name"]) && !empty($_POST["lastName"])) {

    $strSQL = "SELECT * FROM pacientes WHERE Nome = '" . $_POST["name"] . "' AND Sobrenome = '" . $_POST["lastName"] . "'";
    $rs = mysql_query($strSQL);
    $row = mysql_fetch_array($rs);
    
    $ident = $row["id"];
    $nome = $row["Nome"];
    $sobrenome = $row["Sobrenome"];
    $telefone1 = $row["Telefone1"];
    $telefone2 = $row["Telefone2"];
    $cpf = $row["CPF"];
    $rg = $row["RG"];
    $email = $row["Email"];
    $plano = $row["Plano_de_Saude"];
    $alergias = $row["Alergias"];
    $deficiencias = $row["Deficiencias"];
}

//Pesquisa por Identificação
if (!empty($_POST["id"])) {

    $strSQL = "SELECT * FROM pacientes WHERE id=" . $_POST["id"];
    $rs = mysql_query($strSQL);
    $row = mysql_fetch_array($rs);
    
    $ident = $row["id"];
    $nome = $row["Nome"];
    $sobrenome = $row["Sobrenome"];
    $telefone1 = $row["Telefone1"];
    $telefone2 = $row["Telefone2"];
    $cpf = $row["CPF"];
    $rg = $row["RG"];
    $email = $row["Email"];
    $plano = $row["Plano_de_Saude"];
    $alergias = $row["Alergias"];
    $deficiencias = $row["Deficiencias"];
}


// Encerra conexão
//mysql_close();
?>

<script>
    function click() {
        document.getElementsByName("formEdit").item(0).style.display = "block";
    }
</script>

<html>
    <html>
        <head>
            <title>Main</title>
            <link rel="stylesheet" href="css/index.css">
        </head>

        <center><table class="pure-table">
                <thead>
                    <tr>
                        <th>Cadastro Clinico</th>
                        <th>Nome</th>
                        <th>Sobrenome</th>
                        <th>CPF</th>
                        <th>RG</th>
                        <th>Telefone 1</th>
                        <th>Telefone 2</th>
                        <th>E-mail</th>
                        <th>Plano de Saude</th>
                        <th>Alergias</th>
                        <th>Deficiencias</th>
                    </tr>
                    <!-- EDITA AQUI \O/ -->
                <tbody>
                    <tr>
                        <td><label> <?php echo $ident; ?></label></td>
                        <td><label> <?php echo $nome; ?></label></td>
                        <td><label> <?php echo $sobrenome; ?></label></td>
                        <td><label> <?php echo $cpf; ?></label></td>
                        <td><label> <?php echo $rg; ?></label></td>
                        <td><label> <?php echo $telefone1; ?></label></td>
                        <td><label> <?php echo $telefone2; ?></label></td>
                        <td><label> <?php echo $email; ?></label></td>
                        <td><label> <?php echo $plano; ?></label></td>
                        <td><label> <?php echo $alergias; ?></label></td>
                        <td><label> <?php echo $deficiencias; ?></label></td>
                    </tr>
                </tbody>
                </thead>
            </table></center>

        <br>
        <center><div><a class="pure-button pure-button-primary" href=javascript:click()>Editar dados do paciente?</a></div></center>

        <center><form class="pure-form pure-form-aligned divs" name="formEdit" style="display:none" action="editPatient.php" method="post">
                <fieldset>
                    <center><legend>Dados Pessoais</legend></center>
                    
                    <div class="pure-control-group">
                        <input name="id" type="hidden" value="<?php echo $ident; ?>">
                    </div>

                    <div class="pure-control-group">
                        <input name="name" type="text" value="<?php echo $nome; ?>">
                    </div>

                    <div class="pure-control-group">
                        <input name="lastName" type="text" value = "<?php echo $sobrenome; ?>">
                    </div>

                    <div class="pure-control-group">
                        <input name="rg" type="number" value = "<?php echo $rg; ?>">
                    </div>

                    <div class="pure-control-group">
                        <input name="cpf" type="number" value = "<?php echo $cpf; ?>">
                    </div>

                    <center><legend>Contatos</legend></center>

                    <div class="pure-control-group">
                        <input name="tel1" type="number" value = "<?php echo $telefone1; ?>">
                    </div>

                    <div class="pure-control-group">
                        <input name="tel2" type="number" value = "<?php echo $telefone2; ?>">
                    </div>

                    <div class="pure-control-group">
                        <input name="email" type="email" value = "<?php echo $email; ?>">
                    </div>

                    <center><legend>Dados Clinicos</legend></center>

                    <!--aqui jaz Registro Clínico      -->

                    <div class="pure-u-1 pure-u-med-1-3">
                        <select name="insurance" class="pure-input-1-2">
                            <option value="<?php echo $plano; ?>"><?php echo $plano; ?></option>
                            <option value="Excelsior">Excelsior</option>
                            <option value="GEAP">GEAP</option>
                            <option value="Unlimited">Unimed</option>
                            <option value="SUS">SUS</option>
                            <option value="Particular">Particular</option>
                        </select>
                    </div>

                    <br>

                    <div class="pure-control-group">
                        <input name="sick1" type="text" value = "<?php echo $alergias; ?>">
                    </div>

                    <div class="pure-control-group">
                        <input name="sick2" type="text" value = "<?php echo $deficiencias; ?>">
                    </div>

                    <button type="submit" class="pure-button pure-button-primary">Cadastrar</button>
                    </div>
                </fieldset>
            </form></center>
    </body>
</html>